package com.ybg.bookrules.controller;

import com.ybg.bookrules.module.dto.MenusDTO;
import com.ybg.bookcommons.dto.ReturnDTO;
import com.ybg.bookrules.module.dto.UserinfosDTO;
import com.ybg.bookrules.module.vo.UserinfosVO;
import com.ybg.bookrules.services.UserinfosService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/rules")
public class RulesCtrl {
    @Resource
    private UserinfosService userinfosService;
    /**
     * 用户使用用户名和密码登录 成功返回用户的完整信息 失败返回null
     *
     * @param loginname 用户名
     * @param loginpwd  密码
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public UserinfosDTO login(String loginname, String loginpwd) {
      return userinfosService.userLogin(loginname, loginpwd);
    }
    /*
    * 用户注册接口
    * @param user 用户信息对象
    * @return
    * */
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public ReturnDTO register(@RequestBody UserinfosVO user){
        boolean res = userinfosService.userRegister(user);
        if (res){
            return ReturnDTO.builder()
                    .status("success")
                    .message("注册成功")
                    .build();
        }else {
            return ReturnDTO.builder()
                    .status("fail")
                    .message("注册失败")
                    .build();
        }
    }
    /*
    * 用户名检查接口
    * @param username
    * @return
    * */
    @RequestMapping(value="/checkname/{username}",method = RequestMethod.GET)
    public ReturnDTO check(@PathVariable String username){
        boolean canRegister = userinfosService.existsUserByUsername(username);
        if (canRegister){
            return ReturnDTO.builder()
                    .status("fail").message("用户名已存在").build();
    }else {
            return ReturnDTO.builder()
                    .status("success").message("用户名可用").build();
        }
    }
}
